﻿using MyFile.Config;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Threading;
using System.Windows.Forms;
using dts = System.Data;

namespace Inventory
{
    public partial class Form1 : Form
    {
        public SqlSugarClient mDb;
        public Thread Main_Thread = null;
        public bool isRun = true;
        public List<Inventory_Data> Inventory_List =new List<Inventory_Data>();
        public List<DBGoodsList> dBGoodsLists = new List<DBGoodsList>();
        public Form1()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;
            Main_Thread = new Thread(ShowData);
            Main_Thread.IsBackground = true;
            ConnectionConfig config = new ConnectionConfig();
            config.ConnectionString = $"server=36.34.160.80;database=MyFile;uid=root;pwd=Youzi520";
            config.DbType = SqlSugar.DbType.MySql;
            config.IsAutoCloseConnection = true;
            config.InitKeyType = InitKeyType.Attribute;
            mDb = new SqlSugarClient(config);
           
            mDb.DbMaintenance.CreateDatabase();
            mDb.CodeFirst.InitTables(typeof(Inventory_Data));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Inventory_Data _Data = new Inventory_Data();
            _Data.templateId = int.Parse(textBox1.Text);
            _Data.doppler = textBox2.Text;
            _Data.purchasePrice=double.Parse(textBox3.Text);
            _Data.quantity = int.Parse(textBox4.Text);
            Inventory_List.Add(_Data);
            mDb.Insertable(_Data).ExecuteCommand();
            this.Invoke(new Action(() =>
            {
                dataGridView1.DataSource = Inventory_List;
            }));
        }

        public void ShowData()
        {
            Inventory_List = mDb.Queryable<Inventory_Data>().ToList();
            DBGoodsList good = null;
            Inventory_Data _Data = null;
            this.Invoke(new Action(() =>
           {
               if (Inventory_List.Count > 0)
               {
                   dataGridView1.DataSource = Inventory_List;
               }
           }));
            while (isRun)
            {
                try
                {
                    dBGoodsLists = mDb.Queryable<DBGoodsList>().ToList();
                    for (int i = 0; i < Inventory_List.Count; i++)
                    {
                        _Data = Inventory_List[i];
                        good = dBGoodsLists.Find(x => x.templateId == _Data.templateId && x.doppler == _Data.doppler);
                        
                        if (good.price != _Data.price && good.doppler == _Data.doppler)
                        {
                            listBox1.Items.Insert(0, $"{_Data.stickerName}价格从{_Data.price}变为{good.price}");
                            _Data.stickerName = good.stickerName;
                            _Data.price = good.price;
                            _Data.salePrice = good.price * 0.99;
                            _Data.afterSaleProfit= _Data.salePrice * _Data.quantity - _Data.purchasePrice * _Data.quantity;
                            _Data.marketPrice = _Data.salePrice * 0.99;
                            _Data.profit = _Data.marketPrice * _Data.quantity - _Data.purchasePrice * _Data.quantity;
                            _Data.salePrice=Math.Round(_Data.salePrice,2);
                            _Data.afterSaleProfit = Math.Round(_Data.afterSaleProfit, 2);
                            _Data.marketPrice = Math.Round(_Data.marketPrice, 2);
                            _Data.profit = Math.Round(_Data.profit, 2);
                            mDb.Updateable(_Data).ExecuteCommand();
                            Inventory_List[i] = _Data;
                            this.Invoke(new Action(() =>
                            {
                                dataGridView1.DataSource = Inventory_List;
                                dataGridView1.Refresh();
                            }));
                        }
                    }
                }
                catch(Exception ex)
                {
                    listBox1.Items.Insert(0, ex.Message);
                }
                Thread.Sleep(500);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Main_Thread.Start();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            isRun = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int result = mDb.Deleteable<Inventory_Data>().Where(x => x.id == int.Parse(textBox5.Text)).ExecuteCommand();
            listBox1.Items.Insert(0, $"删除{result}条");
        }
    }
}
